package com.dragon.mmochat.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dragon.mmochat.message.Mmochat_12_18S;
import com.dragon.mmochat.message.Mmochat_3_12S;
import com.dragon.mmochat.message.Mmochat_3_13S;
import com.dragon.mmochat.message.Mmochat_5_1S;
import com.dragon.mmochat.model.battle.MmochatBattle;
import com.dragon.mmochat.model.battle.MmochatRoundFrame;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.dragon.mmochat.model.role.MmochatPlayer;

//发送大包时进行分包发送
public class SendBigPackUtil {
	private static Logger log = LoggerFactory.getLogger(SendBigPackUtil.class);

	// 一次下发最多几个包裹物品
	public static int maxPackageGridNumPerSend = 10;

	// 一次下发最多几个战斗操作数据
	public static int maxPkActionNumPerSend = 5;

	// 下发更新包裹
	public static void sendBigPack_5_1S(MmochatPlayer me,
			Collection<MmochatPackageGrid> gridsNeedUpdate) {
		List<MmochatPackageGrid> grids = new ArrayList<MmochatPackageGrid>();
		int count = 0;
		for (MmochatPackageGrid grid : gridsNeedUpdate) {
			grids.add(grid);
			count++;
			if (count >= maxPackageGridNumPerSend) {
				Mmochat_5_1S gridPack = new Mmochat_5_1S();
				gridPack.setGrids(grids);
				MmochatUtil.sendCommonPack(me, gridPack);
				// 重置
				grids.clear();
				count = 0;
			}
		}
		if (grids.size() > 0) {
			Mmochat_5_1S gridPack = new Mmochat_5_1S();
			gridPack.setGrids(grids);
			MmochatUtil.sendCommonPack(me, gridPack);
		}
	}

	// 下发仓库
	public static void sendBigPack_12_18S(MmochatPlayer me,
			Collection<MmochatPackageGrid> gridsNeedUpdate) {
		if (gridsNeedUpdate.size() == 0){
			Mmochat_12_18S gridPack = new Mmochat_12_18S();
			gridPack.setStoredMoney(me.getStoredSmall_money());
			MmochatUtil.sendCommonPack(me, gridPack);
			return;
		}
		List<MmochatPackageGrid> grids = new ArrayList<MmochatPackageGrid>();
		int count = 0;
		for (MmochatPackageGrid grid : gridsNeedUpdate) {
			grids.add(grid);
			count++;
			if (count >= maxPackageGridNumPerSend) {
				Mmochat_12_18S gridPack = new Mmochat_12_18S();
				gridPack.setGrids(grids);
				gridPack.setStoredMoney(me.getStoredSmall_money());
				MmochatUtil.sendCommonPack(me, gridPack);
				// 重置
				grids.clear();
				count = 0;
			}
		}
		if (grids.size() > 0) {
			Mmochat_12_18S gridPack = new Mmochat_12_18S();
			gridPack.setGrids(grids);
			gridPack.setStoredMoney(me.getStoredSmall_money());
			MmochatUtil.sendCommonPack(me, gridPack);
		}
	}

	// 下发战斗操作序列
	public static void sendBigPack_pkAction(MmochatPlayer me,
			MmochatBattle battle, Integer petId) {
		MmochatBattle cloneBattle = battle;
		Mmochat_3_12S pack1 = new Mmochat_3_12S();
		pack1.setBattle(cloneBattle);
		pack1.setPetId(petId);
		pack1.setMe(me);
		MmochatUtil.sendCommonPack(me, pack1);
		log.debug("向" + me.getName() + "发送战斗包part1");

		// 分批下发操作序列
		List<MmochatRoundFrame> frames = new ArrayList<MmochatRoundFrame>();
		int count = 0;
		int totalAttackNum = cloneBattle.getFrames().size();
		for (MmochatRoundFrame frame : cloneBattle.getFrames()) {
			frames.add(frame);
			count++;
			if (count >= maxPkActionNumPerSend) {
				Mmochat_3_13S pack2 = new Mmochat_3_13S();
				pack2.setBattle(cloneBattle);
				pack2.setPetId(petId);
				pack2.setTotalAttackNum(totalAttackNum);
				pack2.setFrames(frames);
				pack2.setMe(me);
				MmochatUtil.sendCommonPack(me, pack2);
				log.debug("向" + me.getName() + "发送战斗包part2");
				// 重置
				frames.clear();
				count = 0;
			}
		}
		if (frames.size() > 0) {
			Mmochat_3_13S pack2 = new Mmochat_3_13S();
			pack2.setBattle(cloneBattle);
			pack2.setPetId(petId);
			pack2.setTotalAttackNum(totalAttackNum);
			pack2.setFrames(frames);
			pack2.setMe(me);
			MmochatUtil.sendCommonPack(me, pack2);
			log.debug("向" + me.getName() + "发送战斗包part2");
		}

	}

}
